<?php
namespace app\console\controller\goods;

use app\BaseController;
use app\console\service\goods\GoodsQueryService;
use app\console\service\goods\GoodsService;
// use app\console\validate\GoodsValidate;
use app\dao\goods\GoodsQueryDao;
use app\lib\exception\ParamException;
use app\model\Goods;
use app\Request;
use think\App;
use app\console\controller\goods\GoodsAPISchemes;

/**
 * 控制器是每个url请求最终的执行方法，目的是响应请求
 * 控制器是介于请求和响应之间，所以他只面向请求和响应
 * 面对请求：关注请求的信息，对信息的判断、验证（只验证数据类型和范围）等
 * 面对响应：关注返回什么数据
 * 在业务上只关注要调度哪个程序、方法，不深入业务细节，对调度结果进行响应
 */
class GoodsController extends BaseController
{
    private $schemes;
    private $service;

    public function __construct(GoodsAPISchemes $schemes, GoodsService $service, GoodsQueryService $queryService)
    {
        $this->schemes = $schemes;
        $this->service = $service;
        $this->queryServicee = $queryService;
        parent::construct();
    }

    /**
     * 批量查询
     */
    public function list()
    {
        $result = $this->queryServicee->getList([
            'name' => $this->request->param('keyword'),
            'number' => $this->request->param('keyword'),
            'tags' => $this->request->param('tags'),
            'startCreatTime' => $this->request->param('start_time'),
            'endCreatTime' => $this->request->param('end_time'),
            'all' => $this->request->param('all'),
            'page' => $this->request->param('page'),
            'perPage' => $this->request->param('per_page'),
        ]);

        return $this->returnList_(
            array_map(fn($goods) => $this->schemes->goods($goods), $result['list']),
            $result['pagination']['page'],
            $result['pagination']['limit'],
            $result['pagination']['total'],
        );
    }

    /**
     * 添加货物
     */
    public function create()
    {
        // TODO 获取参数并验证
        // $params = $this->request->param(['name']);
        // try 
        // {
        //     validate(GoodsValidate::class)
        //         ->scene('create')
        //         ->check($params);
        // }
        // catch (ValidateException $e)
        // {
        //     throw new ParamException($e->getMessage());
        // }

//        $goods = $this->service->create($param);
//
//        return $this->returnList_($this->schemes->goods($goods));
    }

    /**
     * 查询货物
     * TODO Goods自动注入
     */
    public function find(Goods $goods)
    {
        return $this->returnList_($this->schemes->goods($goods));
    }

    /**
     * 更新货物
     */
    public function update(Goods $goods)
    {
        // TODO 获取参数并验证
        // $params = $this->request->param(['name']);
        // try 
        // {
        //     validate(GoodsValidate::class)
        //         ->scene('create')
        //         ->check($params);
        // }
        // catch (ValidateException $e)
        // {
        //     throw new ParamException($e->getMessage());
        // }

//        $goods = $this->server->update($params);
//
//        return $this->returnJson($this->schemes->goods($goods));
    }

    public function delete(Goods $goods)
    {
//        $goods = $this->server->delete($goods);
//        return $this->returnJson();
    }

    public function action(Goods $goods)
    {
        $params = $this->request->param(['action', 'data']);
        // $validate = validate(GoodsValidate::class);
        // try 
        // {
        //     // 验证action值，目前有：onSale, offSale, tags
        //     $validate->scene('action')->check($params);

        //     // 根据action验证参数
        //     $validate
        //         ->scene($params['action'])
        //         ->check($params['data']);
        // }
        // catch (ValidateException $e)
        // {
        //     throw new ParamException($e->getMessage());
        // }
//
//        // TODO 动态调用方法，如果不行找其他方法
//        return $this->{"_action__$params['action']"}($goods, $params['data'])
    }

    private function _action__onSale(Goods $goods, $data)
    {
//        // TODO
//
//        return $this->returnJson();
    }
}